Module 1: Programming Fundamentals

Covering Introduction to Programming, Algorithms, and Flowcharts

(i) Introduction to Programming

1.1 The Basic Model of Computation

At its core, every computer program follows a simple, fundamental model known as IPO (Input, Process, Output).

Example: A Calculator
Input: The numbers 5 and 10, and the operation '+'.
Process: The program adds the two numbers together.
Output: The result, 15, is displayed on the screen.

1.2 Algorithms

An algorithm is a finite, step-by-step, well-defined sequence of instructions designed to solve a specific problem or perform a computation. It is the logical blueprint for a program.

Key Characteristics of a Good Algorithm:

1.3 Flowcharts

A flowchart is a graphical or visual representation of an algorithm. It uses standard symbols to depict the sequence of operations and the flow of data from the beginning to the end of a process. It helps in understanding and analyzing the logic of a problem.

1.4 Programming Languages

A programming language is a formal language comprising a set of instructions that produce various kinds of output. They are used to create computer programs.

1.5 Compilation vs. Interpretation

Computers only understand machine code (binary 0s and 1s). High-level code must be translated. There are two main ways to do this:

FeatureCompiler (e.g., C++)Interpreter (e.g., Python)
Process Scans and translates the entire program at once into an executable file. Translates and executes the program line by line.
Output A separate executable file (.exe) which can be run directly. No separate file. The interpreter runs the code directly.
Speed Generally faster execution because the translation is already done. Slightly slower execution because translation happens during runtime.
Error Checking Reports all syntax errors at the end of compilation. Stops execution as soon as it finds the first error.

1.6 Testing & Debugging

Common Types of Errors:

  1. Syntax Errors: Mistakes in the grammar of the programming language (e.g., a typo, a missing parenthesis). The program will not run at all.
  2. Runtime Errors: Errors that occur while the program is running (e.g., trying to divide by zero). These cause the program to crash.
  3. Logical Errors: The program runs without crashing but produces the wrong result. These are often the hardest to find because the code is syntactically correct.

1.7 Documentation

Documentation is the written text that accompanies computer software. It explains what the software does, how it works, and how to use it.

(ii) Algorithms and Flowcharts to Solve Problems

2.1 Flowchart Symbols

Here are the fundamental symbols used in creating flowcharts:

Shape Name Purpose
Start/End Terminator Represents the starting or ending point of the program (e.g., Start, End, Stop).
Process Process Represents a calculation or an operation (e.g., `sum = a + b`).
Input/Output Input/Output Represents getting data from the user (Input) or displaying results (Output).
Decision Decision Represents a point where a decision is made. It has one entry point and two or more exit points (e.g., Yes/No, True/False).
Flow Line / Arrow Indicates the direction of flow and connects the symbols.

2.2 Basic Algorithm/Flowchart Structures

1. Sequential Processing

Instructions are executed one after another in a linear sequence. This is the simplest structure.

Problem: Create an algorithm and flowchart to read two numbers, calculate their sum, and print the result.

Algorithm:

  1. Start
  2. Declare variables: num1, num2, sum.
  3. Read the value of num1.
  4. Read the value of num2.
  5. Calculate the sum: sum = num1 + num2.
  6. Print the value of sum.
  7. End

Flowchart:

Start Read num1 Read num2 sum = num1 + num2 Print sum End

2. Decision-Based Processing (Conditional)

The program chooses between two or more paths based on a condition. This uses the diamond (decision) symbol.

Problem: Create an algorithm and flowchart to check if a number entered by a user is positive or non-positive.

Algorithm:

  1. Start
  2. Declare variable num.
  3. Read the value of num.
  4. If num > 0 then:
  5.     Print "Number is Positive"
  6. Else:
  7.     Print "Number is Non-Positive"
  8. End If
  9. End

Flowchart:

Start Read num Is num > 0? Print "Positive" Yes Print "Non-Positive" No End

3. Iterative Processing (Looping)

A set of instructions is repeated either a fixed number of times or until a certain condition is met. This creates a loop in the flowchart.

Problem: Create an algorithm and flowchart to find the sum of the first 5 natural numbers (1 to 5).

Algorithm:

  1. Start
  2. Initialize variables: sum = 0, counter = 1.
  3. Repeat the following steps while counter <= 5:
  4.     a. Add counter to sum: sum = sum + counter.
  5.     b. Increment counter: counter = counter + 1.
  6. Print the final sum.
  7. End

Flowchart:

Start sum = 0, counter = 1 Is counter <= 5? Yes sum = sum + counter counter = counter + 1 No Print sum End

2.3 Solved Examples

Exchanging Values of Two Variables

A classic problem that demonstrates sequential processing and the need for a temporary storage location.

Problem: Given two variables, A and B, swap their values. (e.g., if A=10 and B=20, after swapping A should be 20 and B should be 10).

Algorithm (Using a third variable):

  1. Start
  2. Declare variables: A, B, TEMP.
  3. Read values for A and B.
  4. Copy the value of A into TEMP: TEMP = A.
  5. Copy the value of B into A: A = B.
  6. Copy the value of TEMP into B: B = TEMP.
  7. Print the new values of A and B.
  8. End

Flowchart:

Start Read A, B TEMP = A A = B B = TEMP Print A, B End